package yingyong;

import gongju.DBUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/insert")
public class Insert extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf8");
        response.setContentType("text/html;charset=utf8");
        String id = request.getParameter("id");
        String mode = request.getParameter("mode");
        String rent = request.getParameter("rent");
        String cont = request.getParameter("cont");
        String method = request.getParameter("method");
        int house = Integer.parseInt(request.getParameter("type"));
        String addr = request.getParameter("addr");
        String sql = "insert into house_info values (0,?,?,?,?,?,?)";
        int i = 0;
        try {
            i = DBUtil.update(sql,mode, rent, cont, method, house, addr);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        if (i>0){
            response.getWriter().write("<script>alert('添加成功');location.href='/select';</script>");
// resp.getWriter().write("<script>alert('添加成功');location.href='/select';</script>");
        }else {
            response.getWriter().write("添加失败");
        }
    }
}
